package com.skeleton.adminServer.user.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skeleton.adminServer.user.service.IAdminUserService;
import com.skeleton.adminServer.user.util.AdminUserConstants;
import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.db.dto.PageDto;
import com.skeleton.db.dto.UserinfoDto;

@Controller
@RequestMapping("admin/user/")
public class AdminUserControl {

	protected Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private IAdminUserService iAdminUserService;

	@RequestMapping("adminAjaxLogin")
	@ResponseBody
	public Map<String, Object> adminAjaxLogin(HttpServletRequest request, HttpSession session) {
		logger.info("-------admin/user/---adminAjaxLogin-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String loginname = request.getParameter("loginname");
		String password = request.getParameter("password");

		UserinfoDto uDto = new UserinfoDto();
		uDto.setUserLoginname(loginname);
		uDto.setUserPassword(password);

		ResultDto rd = iAdminUserService.doLogin(uDto);
		if (rd.isSuccess()) {
			UserinfoDto ud = (UserinfoDto) rd.getFirstDto("UserinfoDto");
			session.setAttribute(AdminUserConstants.LOGINUSER_SESSION_NAME_ADMIN, ud);
			map.put("loginname", loginname);
			map.put("result", AdminUserConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminUserConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("adminAjaxLogout")
	@ResponseBody
	public Map<String, Object> adminAjaxLogout(HttpServletRequest request, HttpSession session) {
		logger.info("-------admin/user/---adminAjaxLogout-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		session.removeAttribute(AdminUserConstants.LOGINUSER_SESSION_NAME_ADMIN);

		map.put("result", AdminUserConstants.AJAX_SUCCESS);
		return map;
	}

	@RequestMapping("adminAjaxLoadUser")
	@ResponseBody
	public Map<String, Object> adminAjaxLoadUser(HttpServletRequest request, HttpSession session) {
		logger.info("-------admin/user/---adminAjaxLoadUser-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		PageDto pDto = new PageDto(Integer.parseInt(page), Integer.parseInt(rows));

		ResultDto rd = iAdminUserService.getAllUserByPage(pDto);
		if (rd.isSuccess()) {
			List<UserinfoDto> udList = rd.getDtoList("UserinfoDtoList");
			map.put("rows", udList);
			pDto = (PageDto) rd.getFirstDto("PageDto");
			map.put("total", pDto.getTotal());
			map.put("result", AdminUserConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminUserConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

}
